
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using Swaf.BizObj;
using Swaf.Security;
using Swaf.Gui.AspNet2;

namespace Syrinx.Gui.AspNet
{
    [ParseChildren(false)]
    public class DynamicTab : Swaf.Gui.AspNet2.UserControl
    {
        protected IList m_tabedElements = new ArrayList();
        protected int m_selectedTab = 0;
        protected int m_curProcessingTab = 0;

        protected string m_selectedTabStyleClass = "";
        protected string m_unselectedTabStyleClass = "";
				
		protected string m_templateName = "DynamicTab";
		protected static string s_template = "{0}.{1}.ascx";


        public string SelectedTabClass { get { return m_selectedTabStyleClass; } set { m_selectedTabStyleClass = value; } }
        public string UnselectedTabClass { get { return m_unselectedTabStyleClass; } set { m_unselectedTabStyleClass = value; } }

        protected override void OnLoad(EventArgs e)
        {
            if (Page.Request.Form["__EVENTTARGET"] == UniqueID)
            {
                m_selectedTab = Int32.Parse(Page.Request.Form["__EVENTARGUMENT"]);
                ViewState["SID"] = m_selectedTab;
            }
            else
                if (ViewState.Count > 0)
                    m_selectedTab = (int)ViewState["SID"];
            Page.LoadComplete += new EventHandler(Page_LoadComplete);
            base.OnLoad(e);
            if (TabSelected != null)
                TabSelected(this, new EventArgs());
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (Page.Request.Form["__EVENTTARGET"] == UniqueID && TabSelected != null)
                TabSelected(this, new EventArgs());
        }

		protected override System.IO.TextReader Html
		{
			get
			{
				return new StreamReader(this.GetType().Assembly.GetManifestResourceStream(ContentName));
			}
		}

		public override string ContentName
		{
			get
			{
				return string.Format(s_template, this.GetType().Namespace,
				   (m_templateName == null) ? this.GetType().Name : m_templateName);
			}
		}

		public string Template
		{
			get { return m_templateName; }
			set { m_templateName = value; }
		}

        public Control SelectedControl 
        { get { return ((Tab)m_tabedElements[m_selectedTab]).TabChild; } }

        public event EventHandler TabSelected;

        public void generateTabs(HtmlBlockInfo info)
        {
            int numCtls = Controls.Count;
            for (int pos = 0; pos < numCtls; ++pos)
            {
                string tabHtml = "";
                Control ctl = Controls[pos];
                if (ctl is LiteralControl)
                {
                    tabHtml = ((LiteralControl)ctl).Text.Trim();
                    if (++pos >= numCtls)
                        break;
                    ctl = Controls[pos];
                }
                Tab t = new Tab(tabHtml, ctl);
                int tabId = m_tabedElements.Count;
                info.generateBlock(tabId.ToString());
                m_tabedElements.Add(t);

                System.Web.UI.WebControls.Label generatedTab = FindControl("tab" + tabId) as System.Web.UI.WebControls.Label;
                generatedTab.Attributes.Add("onclick",
                    "__doPostBack('" + UniqueID + "', '" + tabId + "')");
                if (generatedTab != null)
                    generatedTab.Text = t.TabHtml;
            }
        }

        public string getTabStyleClass()
        {
            return (m_curProcessingTab++ == m_selectedTab)?SelectedTabClass:UnselectedTabClass;
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            bool allTabsInvisible = true;
            foreach (Tab t in m_tabedElements)
                if (t.TabChild.Visible)
                {
                    allTabsInvisible = false;
                    break;
                }
            if (allTabsInvisible)
                Visible = false;
            else
            {
                createControls();
                Panel mainArea = FindControl("MainArea") as Panel;
                Tab selectedTab = m_tabedElements[m_selectedTab] as Tab;
                if (mainArea != null)
                    mainArea.Controls.Add(selectedTab.TabChild);
                base.RenderContents(output);
            }
        }

        protected class Tab
        {
            public string TabHtml;
            public Control TabChild;
            public Tab(string tabHtml, Control tabChild)
            {
                TabHtml = tabHtml;
                TabChild = tabChild;
            }
        }
    }
}
